package com.lft.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * Function: 		ADD FUNCTION.
 * Reason:   		ADD REASON.
 * Date:            2020-11-20 10:31
 * <p>
 * Class Name:      ListValueConstraintValidator
 * Package Name:    com.lft.common.valid
 * @author Laifengting / E-mail:laifengting@foxmail.com
 * @version 1.0.0
 * @since JDK 8
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
	private Set<Integer> set = new HashSet<>();
	
	@Override
	public void initialize(ListValue constraintAnnotation) {
		int[] values = constraintAnnotation.values();
		for (int value : values) {
			set.add(value);
		}
	}
	
	/**
	 * 判断是否校验成功
	 * @param value   需要校验的值
	 * @param context
	 * @return
	 */
	@Override
	public boolean isValid(Integer value, ConstraintValidatorContext context) {
		return set.contains(value);
	}
}
